﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Forums.SqlServer.Repositories.Readers
{
	using System.Data.SqlClient;
	using Falcon.Core.Domain;
	using Falcon.Core.SqlServer.Repositories.Readers;
	using Falcon.Forums.Domain;
	using Falcon.Forums.Domain.Enums;

	public class ForumTopicSubscriptionSingleReader : IReader<ForumTopicSubscription>
	{
		/// <summary>
		/// Populates the specified reader.
		/// </summary>
		/// <param name="reader">The reader.</param>
		/// <param name="entity">The entity.</param>
		public void Populate(SqlDataReader reader, ForumTopicSubscription entity)
		{
			entity.Id = reader.GetInt32(0);
			entity.Notification = (ForumTopicSubscriptionNotification)reader.GetInt32(1);

			entity.User = new User
			{
				Id = reader.GetInt32(2),
				Name = reader.GetString(3),
				EmailAddress = reader.GetString(4),
				Slug = reader.GetString(5),
				Reputation = reader.GetInt32(6),
				Profile = new UserProfile { Title = reader.GetString(7) }
			};

			entity.Topic = new ForumTopic
			{
				Id = reader.GetInt32(8),
				Slug = reader.GetString(9),
				Replies = reader.GetInt32(10),
				Views = reader.GetInt32(11),
				Scope = (ForumTopicScope)reader.GetInt32(12),
				Status = (ForumTopicStatus)reader.GetInt32(13),
				Created = reader.GetDateTime(14),
				Updated = reader.GetDateTime(15),
				Forum = new Forum
				{
					Id = reader.GetInt32(16),
					Title = reader.GetString(17),
					Slug = reader.GetString(18),
				},
				Post = new ForumPost
				{
					Id = reader.GetInt32(19),
					Title = reader.GetString(20),
					Slug = reader.GetString(21),
					Created = reader.GetDateTime(22),
					Updated = reader.GetDateTime(23),
					User = new User
					{
						Id = reader.GetInt32(24),
						Name = reader.GetString(25),
						Slug = reader.GetString(26),
						Reputation = reader.GetInt32(27),
						Profile = new UserProfile { Title = reader.GetString(28) }
					}
				}
			};

			if (!reader.IsDBNull(29))
			{
				entity.Topic.Post.User.Profile.Avatar = new UserAvatar
				{
					FileName = reader.GetString(29),
					FileDiskName = reader.GetString(30),
					FilePath = reader.GetString(31),
					FileType = reader.GetString(32)
				};
			}

			if (reader.IsDBNull(33))
			{
				return;
			}

			entity.Topic.LastPost = new ForumPost
			{
				Id = reader.GetInt32(33),
				Title = reader.GetString(34),
				Slug = reader.GetString(35),
				BodyParsed = reader.GetString(36),
				Created = reader.GetDateTime(37),
				Updated = reader.GetDateTime(38),
				User = new User
				{
					Id = reader.GetInt32(39),
					Name = reader.GetString(40),
					Slug = reader.GetString(41),
					Reputation = reader.GetInt32(42),
					Profile = new UserProfile { Title = reader.GetString(43) }
				}
			};

			if (!reader.IsDBNull(44))
			{
				entity.Topic.LastPost.User.Profile.Avatar = new UserAvatar
				{
					FileName = reader.GetString(44),
					FileDiskName = reader.GetString(45),
					FilePath = reader.GetString(46),
					FileType = reader.GetString(47)
				};
			}
		}
	}
}